package com.zhkj.txg.module.member.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.zhkj.lib.base.BaseViewModel
import com.zhkj.lib.http.HttpResponse
import com.zhkj.lib.http.ViewModelSubscribeListener
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.module.member.entity.MemberCalendarResponse
import com.zhkj.txg.module.member.entity.RewardCenterResponse
import com.zhkj.txg.module.member.model.MemberManager
import java.text.SimpleDateFormat
import java.util.*

class MemberCenterViewModel(application: Application) : BaseViewModel(application) {
    private val manager = MemberManager()
    val taskLiveData = MutableLiveData<HttpResponse<RewardCenterResponse>>()
    val calendarLiveData = MutableLiveData<HttpResponse<MemberCalendarResponse>>()

    // 日历对象
    val calendar: Calendar = Calendar.getInstance()
    private val pattern = "yyyy年MM月"

    fun getTask() {
        val listener = object : ViewModelSubscribeListener<RewardCenterResponse>(taskLiveData) {
            override fun onSuccess(resp: RewardCenterResponse) {
                super.onSuccess(resp)
                calendar()
            }
        }
        val rewardCenter = manager.rewardCenter(AccountManager.getToken(), listener)
        dispose.add(rewardCenter)
    }

    fun calendar() {
        val listener = ViewModelSubscribeListener(calendarLiveData)
        val calender = manager.memberCalender(AccountManager.getToken(), getFormatStr(), listener)
        dispose.add(calender)
    }

    fun getUICalendarStr(): String = SimpleDateFormat(pattern, Locale.getDefault()).format(calendar.time)

    private fun getFormatStr(): String = SimpleDateFormat("yyyy-MM", Locale.getDefault()).format(calendar.time)
}